package com.htthorizon.system.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import com.htthorizon.common.core.constant.Constants;
import com.htthorizon.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.htthorizon.common.core.constant.CacheConstants;
import com.htthorizon.common.core.utils.StringUtils;
import com.htthorizon.common.core.web.controller.BaseController;
import com.htthorizon.common.core.web.domain.AjaxResult;
import com.htthorizon.common.core.web.page.TableDataInfo;
import com.htthorizon.common.log.annotation.Log;
import com.htthorizon.common.log.enums.BusinessType;
import com.htthorizon.common.redis.service.RedisService;
import com.htthorizon.common.security.annotation.RequiresPermissions;
import com.htthorizon.system.api.model.LoginUser;
import com.htthorizon.system.domain.SysUserOnline;
import com.htthorizon.system.service.ISysUserOnlineService;

/**
 * 在线用户监控
 * 
 * @author mark
 */
@RestController
@RequestMapping("/online")
public class SysUserOnlineController extends BaseController
{
    @Autowired
    private ISysUserOnlineService userOnlineService;

    @Autowired
    private RedisService redisService;

    @RequiresPermissions("monitor:online:list")
    @GetMapping("/list")
    @Log(title = "系统管理/在线用户查询", businessType = BusinessType.QUERY,type = Constants.SYSTEM)
    public TableDataInfo list(String ipaddr, String userName)
    {
        Collection<String> keys = redisService.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
        List<SysUserOnline> userOnlineList = new ArrayList<SysUserOnline>();
        for (String key : keys){
            Object object = redisService.getCacheObject(key);
            if(object instanceof LoginUser) {
                LoginUser user = redisService.getCacheObject(key);
                if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName)) {
                    userOnlineList.add(userOnlineService.selectOnlineByInfo(ipaddr, userName, user));
                } else if (StringUtils.isNotEmpty(ipaddr)) {
                    userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipaddr, user));
                } else if (StringUtils.isNotEmpty(userName)) {
                    userOnlineList.add(userOnlineService.selectOnlineByUserName(userName, user));
                } else {
                    userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
                }
            }else {
                logger.warn("在线用户异常：" +object);
            }
        }
        Collections.reverse(userOnlineList);
        userOnlineList.removeAll(Collections.singleton(null));
        return getDataTable(userOnlineList);
    }

    /**
     * 强退用户
     */
    @RequiresPermissions("monitor:online:forceLogout")
    @Log(title = "系统管理/在线用户", businessType = BusinessType.FORCE,type = Constants.SYSTEM)
    @PostMapping("/{tokenId}")
    public AjaxResult forceLogout(@PathVariable String tokenId)
    {
        redisService.deleteObject(CacheConstants.LOGIN_TOKEN_KEY + tokenId);
        return success();
    }

    public static void main(String[] args) {
        String s = SecurityUtils.encryptPassword("我和你");
        String str="$2a$05$QEkpejlCfmBtj01fSkb.r.p8EBa5dVgGCK9.2UYNIdJ6Q5Z2iJk4O";
        boolean b = SecurityUtils.matchesPassword("pwd", str);
        System.out.println(b);
        System.out.println(s);
    }
}
